<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :edit="edit" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		errorModal,
		getDateStr
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				customerCacheKey:'rps-tz-customerOwnerAdjustment-customer-cache',
				ownerCacheKey:'rps-tz-owner-customer-cache',
				customerShowId:'',
				originalMainOwnerArr:[],
				exists:[]
			})
		},
		methods:{
			onSelectPos(item){
				let _that = this
				_that.$refs.autoform.formdata.instNameId = item.instId
				_that.$refs.autoform.formdata.instName = item.instName
			},
			findPostList(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/findPostList",
					{
						userId:id
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title1","postId","data",configSelectData(res,'code','posName'))
					_that.$refs.autoform.changeEditor("title1","postId","type","selectbox")
					_that.$refs.autoform.changeEditor("title1","postId","prompt","请输入")
					_that.$refs.autoform.changeEditor("title1","postId","onSelect","onSelectPos")
					_that.$refs.autoform.initSelectIndex("title1","postId")
				})
			},
			onSelectArrangeMode(item){
				let _that = this
				_that.$refs.autoform.clear("title1","ownerNameId")
				_that.$refs.autoform.clear("title1","postId")
				_that.$refs.autoform.clear("title1","instName")
				_that.$refs.autoform.clear("title1","principalOwner")
				_that.$refs.autoform.clear("title1","originalMainOwner")
				
				_that.$refs.autoform.changeEditor("title1","ownerNameId",{
								link:'../../public/search/search',
								"linkParam":{
									other:JSON.stringify({
										customerShowId:_that.customerShowId,
										arrangeMode:item.code,
										filterUserId:''
									}),
									url:'customerOwnerAdjustment/getCustomerOwner',
									namefield:'customerOwnerName',
									initload:true,
									searchfield:'q',
									cachename:_that.ownerCacheKey
								}
							})
				if(item.code != '36870'){
					_that.$refs.autoform.changeEditor("title1","principalOwner",'data',configSelectData(_that.originalMainOwnerArr,'code','dicname'))
					_that.$refs.autoform.changeEditor("title1","principalOwner",'type','selectbox')
				}else{
					_that.$refs.autoform.changeEditor("title1","principalOwner",'type','labelbox')
				}
				if(item.code != '36869'){
					_that.$refs.autoform.changeEditor("title1","postId","type","labelbox")
				}
			},
			initload(){
				let _that = this
				let arr = [
					_that.om.request(
						'dictZero/getCombobox',
						{
							code:'36868'
						}
					),
					_that.om.request(
						"dictZero/getCombobox",
						{
							code:'38400'
						}
					)
				]
				Promise.all(arr).then(res=>{
					_that.$refs.autoform.changeEditor("title1","arrangeMode","data",configSelectData(res[0],"code","dicname"))
					_that.$refs.autoform.initSelectIndex("title1","arrangeMode")
					_that.originalMainOwnerArr = res[1]
					if(_that.$refs.autoform.formdata.ownerNameId && _that.formdata.arrangeMode ==36869){
						_that.findPostList(_that.$refs.autoform.formdata.ownerNameId)
					}
					if(_that.formdata.arrangeMode && _that.formdata.arrangeMode != '36870'){
						_that.$refs.autoform.changeEditor("title1","principalOwner",'data',configSelectData(res[1],'code','dicname'))
						_that.$refs.autoform.initSelectIndex("title1","principalOwner")
					}
				})
			},
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				if (e.configDataCacheKey) {
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if (res.data && res.data instanceof Array) {
							_that.exists = _that.datas.filter((e,i)=>{
								return (i != _that.index)
							}).map(e=>e.ownerNameId).join(",")
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
							}
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							changeEditor('title1',"customerCodeId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({}),
									url:'customerOwnerAdjustment/getCustomer',
									namefield:'customerShortName',
									notefield:'customerCode',
									initload:true,
									searchfield:'search',
									cachename:_that.customerCacheKey
								}
							},null,_that.autoformdata)
							if(_that.formdata.customerCode){
								_that.customerShowId = _that.formdata.customerCode
							}
							changeEditor('title1',"ownerNameId",{
								link:'../../public/search/search',
								"linkParam":{
									other:JSON.stringify({
										customerShowId:_that.customerShowId||'',
										arrangeMode:_that.formdata.arrangeMode || '',
										filterUserId:_that.exists
									}),
									url:'customerOwnerAdjustment/getCustomerOwner',
									namefield:'customerOwnerName',
									initload:true,
									searchfield:'q',
									cachename:_that.ownerCacheKey
								}
							},null,_that.autoformdata)
							
							_that.initload()
						}
					})
				}
			})
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key:_that.customerCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.customerCodeId = d.customerCode
							formdata.customerCode = d.id
							formdata.customerCode2 = d.customerId
							formdata.customerAbbreviation = d.customerShortName
							formdata.customerName = d.customerName
							formdata.developmentDate = d.developmentDate
							_that.customerShowId = d.id
							uni.removeStorage({
								key:_that.customerCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.ownerCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.ownerNameId = d.customerOwnerId
							formdata.ownerName = d.customerOwnerName
							if(_that.$refs.autoform.formdata.arrangeMode == '36869'){
								_that.$refs.autoform.formdata.originalMainOwnerName = _that.originalMainOwnerArr[1].dicname
								_that.$refs.autoform.formdata.originalMainOwner = _that.originalMainOwnerArr[1].code
								_that.findPostList(d.customerOwnerId)
							}else{
								_that.$refs.autoform.changeEditor("title1","postId","type","labelbox")
								_that.$refs.autoform.formdata.originalMainOwnerName = _that.originalMainOwnerArr[0].dicname
								_that.$refs.autoform.formdata.originalMainOwner = _that.originalMainOwnerArr[0].code
								if(_that.$refs.autoform.formdata.arrangeMode == '36870'){
									errorModal(_that,'提示信息','是否要取消'+_that.$refs.autoform.formdata.ownerName+'作为'+_that.$refs.autoform.formdata.customerAbbreviation+'客户的所属人!',()=>{
										
									})
								}else{
									_that.$refs.autoform.formdata.instNameId = d.instId
									_that.$refs.autoform.formdata.instName = d.instName
									_that.$refs.autoform.formdata.postId = d.postId
									_that.$refs.autoform.formdata.postName = d.postName
									errorModal(_that,'提示信息',_that.$refs.autoform.formdata.ownerName+'当前为'+_that.$refs.autoform.formdata.customerAbbreviation+'客户的主要所属人,是否要调整'+_that.$refs.autoform.formdata.ownerName+'为非主要所属人',()=>{
										
									})
								}
							}
							
							uni.removeStorage({
								key:_that.ownerCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
